import { Component } from '@angular/core';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { NzMessageService } from 'ng-zorro-antd/message';
import { _HttpClient } from '@delon/theme';
import { map } from 'rxjs/operators';
import { environment } from '@env/environment';
import { EditComponent } from 'src/app/routes/common/edit/edit.component';
import { SysUser } from '@api/sys/model/sysUser.model';
import { SysUserService } from '@api/sys/service/sysUser.service';
import { SysKeyValueService } from '@api/sys/service/sysKeyValue.service';

@Component({
  selector: 'app-sys-user-edit',
  templateUrl: '../../../common/edit/edit.component.html',
})
export class SysUserEditComponent extends EditComponent<SysUser, String> {
  constructor(
    public modal: NzModalRef,
    public msgSrv: NzMessageService,
    public http: _HttpClient,
    public sysUserService: SysUserService,
    public keyValueService: SysKeyValueService,
  ) {
    super(modal, msgSrv, http, sysUserService);
    this.schema = {
      properties: {
        username: { type: 'string', title: '用户名', maxLength: 30 },
        petname: { type: 'string', title: '昵称', maxLength: 30 },
        realname: { type: 'string', title: '真实姓名', maxLength: 30 },
        gender: {
          type: 'string',
          title: '性别',
          maxLength: 15,
          ui: {
            widget: 'radio',
            asyncData: () => this.keyValueService.getSFSchemaEnumTypesByType('/USER/GENDER'),
            default: 'N',
          },
        },
        birthday: { type: 'string', title: '生日', maxLength: 15, format: 'date' },
        phone: { type: 'string', title: '电话', maxLength: 15, format: 'mobile' },
        email: { type: 'string', title: '邮箱', maxLength: 40, format: 'email' },
        roleIds: { type: 'string', title: '角色', maxLength: 15 },
      },
      required: ['username', 'roleIds'],
    };
    this.ui = {
      '*': {
        spanLabelFixed: 100,
        grid: { span: 12 },
      },
      $roleIds: {
        grid: { span: 24 },
        widget: 'transfer',
        titles: ['未拥有', '已拥有'],
        asyncData: () =>
          this.http.get(`${environment.sys.ctx}/sysRole/findAll`).pipe(
            map((items) => {
              const options: any[] = [];
              items.forEach((element: any) => {
                options.push({ title: element.name, value: element.id });
              });
              return options;
            }),
          ),
      },
    };
  }
}
